package com.tianhai.utils;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.tianhai.pojo.Plan;
import com.tianhai.pojo.PlanVo;
import com.tianhai.pojo.PlanVo2;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.*;

/**
 * 动态更改注解类属性值
 */
public class DynamicChangeAnnoUtil {

    /**
     * 获得当前后六个月
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void changeAnnoValue() throws NoSuchFieldException, IllegalAccessException {
        // 初始定义 字段名集合
        List<String> fieldList = Arrays.asList("field1", "field2", "field3", "field4", "field5", "field6");

        // 获得当前后六个月
        List<String> afterSixMonth = DateUtil.sixMonthsAfterObtaining();

        // 获得Plan类中所有的字段属性
        Field[] fields = Plan.class.getDeclaredFields();

        // 遍历所有的字段属性
        for (Field f : fields) {
            // 遍历初始字段名集合
            for (int i = 0; i < fieldList.size(); i++) {
                // 获得字段名
                String item = fieldList.get(i);

                // 遍历的字段属性与初始字段集合一致
                if (item.trim().equals(f.getName().trim())) {
                    //获取val字段上的Foo注解实例
                    Excel foo = f.getAnnotation(Excel.class);
                    //获取 foo 这个代理实例所持有的 InvocationHandler
                    InvocationHandler h = Proxy.getInvocationHandler(foo);
                    // 获取 AnnotationInvocationHandler 的 memberValues 字段
                    Field hField = null;
                    try {
                        hField = h.getClass().getDeclaredField("memberValues");
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    }
                    // 因为这个字段事 private final 修饰，所以要打开权限
                    hField.setAccessible(true);
                    // 获取 memberValues
                    Map memberValues = null;
                    try {
                        memberValues = (Map) hField.get(h);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    // 修改 value 属性值
                    memberValues.put("name", afterSixMonth.get(i));
                }
            }
        }

    }

    /**
     * 获得当前后六个月，及差异值
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void changeAnnoValueConvert() throws NoSuchFieldException, IllegalAccessException {
        // 初始定义 字段名集合
        List<String> fieldList = Arrays.asList("field1", "field2", "difference1", "field3", "field4", "difference2", "field5",
                "field6", "difference3", "field7", "field8", "difference4"
                , "field9", "field10", "difference5", "field11", "field12", "difference6");

        // 获得当前后六个月
        List<String> afterSixMonth = DateUtil.sixMonthsAfterObtaining();

        List<String> afterSixMonth1 = DateUtil.sixMonthsAfterObtaining();

        // 创建差异值
        List<String> differences = new ArrayList<>();

        // 添加差异值
        afterSixMonth.forEach(item -> {
            differences.add(item + "差异");
        });

        List<String> result = new ArrayList<>();
        for (int i = 0; i < afterSixMonth.size(); i++) {
            result.add(afterSixMonth.get(i));
            result.add(afterSixMonth1.get(i));
            result.add(differences.get(i));
        }

        // 结果排序
        Collections.sort(result);

        // 获得Plan类中所有的字段属性
        Field[] fields = PlanVo.class.getDeclaredFields();

        // 遍历所有的字段属性
        for (Field f : fields) {
            // 遍历初始字段名集合
            for (int i = 0; i < fieldList.size(); i++) {
                // 获得字段名
                String item = fieldList.get(i);

                // 遍历的字段属性与初始字段集合一致
                if (item.trim().equals(f.getName().trim())) {
                    //获取val字段上的Foo注解实例
                    Excel foo = f.getAnnotation(Excel.class);
                    //获取 foo 这个代理实例所持有的 InvocationHandler
                    InvocationHandler h = Proxy.getInvocationHandler(foo);
                    // 获取 AnnotationInvocationHandler 的 memberValues 字段
                    Field hField = null;
                    try {
                        hField = h.getClass().getDeclaredField("memberValues");
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    }
                    // 因为这个字段事 private final 修饰，所以要打开权限
                    hField.setAccessible(true);
                    // 获取 memberValues
                    Map memberValues = null;
                    try {
                        memberValues = (Map) hField.get(h);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    // 修改 value 属性值
                    memberValues.put("name", result.get(i));
                }
            }
        }
    }

    /**
     * 获得后六个月差异明细
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void changeAnnoValueDetail() throws NoSuchFieldException, IllegalAccessException {
        // 初始定义 字段名集合
        List<String> fieldList = Arrays.asList("field1", "field2", "field3", "field4", "field5", "field6",
                "fieldNew1", "fieldNew2", "fieldNew3", "fieldNew4", "fieldNew5", "fieldNew6",
                "fieldOld1", "fieldOld2", "fieldOld3", "fieldOld4", "fieldOld5", "fieldOld6");

        // 获得当前后六个月
        List<String> afterSixMonth = DateUtil.sixMonthsAfterObtaining();

        // 遍历添加求和项
        List<String> afterSixMonthSumProject = DateUtil.sixMonthsAfterObtaining();
        for (int i = 0; i < afterSixMonthSumProject.size(); i++) {
            afterSixMonthSumProject.get(i);
            afterSixMonthSumProject.set(i, "求和项:" + afterSixMonthSumProject.get(i));
        }


        afterSixMonth.addAll(afterSixMonthSumProject);
        afterSixMonth.addAll(afterSixMonthSumProject);


        // 获得Plan类中所有的字段属性
        Field[] fields = PlanVo2.class.getDeclaredFields();

        // 遍历所有的字段属性
        for (Field f : fields) {
            // 遍历初始字段名集合
            for (int i = 0; i < fieldList.size(); i++) {
                // 获得字段名
                String item = fieldList.get(i);

                // 遍历的字段属性与初始字段集合一致
                if (item.trim().equals(f.getName().trim())) {
                    //获取val字段上的Foo注解实例
                    Excel foo = f.getAnnotation(Excel.class);
                    //获取 foo 这个代理实例所持有的 InvocationHandler
                    InvocationHandler h = Proxy.getInvocationHandler(foo);
                    // 获取 AnnotationInvocationHandler 的 memberValues 字段
                    Field hField = null;
                    try {
                        hField = h.getClass().getDeclaredField("memberValues");
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    }
                    // 因为这个字段事 private final 修饰，所以要打开权限
                    hField.setAccessible(true);
                    // 获取 memberValues
                    Map memberValues = null;
                    try {
                        memberValues = (Map) hField.get(h);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    // 修改 value 属性值
                    memberValues.put("name", afterSixMonth.get(i));
                }
            }
        }

    }
}